- Vous travaillez pour un organisme de presse.
- Vous devez fournir ttes les analyses et les viz correspondantes aux résultats du 1er tour et du 2è tour.
- Surtout des cartes géo interactives (stylé et sympa)
- Soyez curieux et inventifs
- N.B : le notebook est un support (pr vs aider) et non le projet final
Rq : pourquoi ne pas utiliser Flask pr les viz sur Elections Presidentielles
#attention installer plotky 3.1 pas la derniere version
#conda install -c plotly plotly=3.10.0
import matplotlib
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from pandas.plotting import scatter_matrix
#pour faire la carte interative
import folium
from folium.plugins import FastMarkerCluster
import geopandas as gpd
from branca.colormap import LinearColormap
#corrélations
import seaborn as sns
sns.set()
#ouvir dansle navigateur
import webbrowser
matplotlib.__version__
%matplotlib inline
plt.style.use('ggplot')
all_sheet_in_odict_df = pd.read_excel('source/resultat-elections-2012.xls', sheet_name = None, index_col = None)
all_sheet_in_odict_df.keys()
all_sheet_in_odict_df['Circo leg T1'].head()
tour1 = pd.read_excel('source/resultat-elections-2012.xls', sheet_name = 'Départements T1', index_col = None)
tour1['Code du département'].replace({'ZA':'971','ZB':'972','ZC':'973','ZD':'974','ZM':'976'}, inplace=True)
tour2 = pd.read_excel('source/resultat-elections-2012.xls', sheet_name = 'Départements T2', index_col = None)
tour2['Code du département'].replace({'ZA':'971','ZB':'972','ZC':'973','ZD':'974','ZM':'976'}, inplace=True)
tour1_2 = tour1.merge(tour2, on = 'Code du département', suffixes = ('T1', 'T2'))
tour1_2["rHollandeT1"] = tour1_2['Voix.9'] / (tour1_2["VotantsT1"] - tour1_2["Blancs et nulsT1"])
tour1_2["rSarkozyT1"] = tour1_2['Voix.2'] / (tour1_2["VotantsT1"] - tour1_2["Blancs et nulsT1"])
tour1_2["rNulT1"] = tour1_2["Blancs et nulsT1"] / tour1_2["VotantsT1"]
tour1_2["rHollandeT2"] = tour1_2["VoixT2"] / (tour1_2["VotantsT2"] - tour1_2["Blancs et nulsT2"])
tour1_2["rSarkozyT2"] = tour1_2['Voix.1T2'] / (tour1_2["VotantsT2"] - tour1_2["Blancs et nulsT2"])
tour1_2["rNulT2"] = tour1_2["Blancs et nulsT2"] / tour1_2["VotantsT2"]
data = tour1_2[["Code du département", "Libellé du départementT1",
"VotantsT1", "rHollandeT1", "rSarkozyT1", "rNulT1",
"VotantsT2", "rHollandeT2", "rSarkozyT2", "rNulT2"]]
tour1_2.to_excel("source/temp/tour1_2.xlsx")
tour1.to_excel("source/temp/tour1.xlsx")
tour2.to_excel("source/temp/tour2.xlsx")
data.to_excel("source/temp/data.xlsx")
dix=data.iloc[0:10]
vingt=data.iloc[10:20]
trente=data.iloc[20:30]
quarante=data.iloc[30:40]
cinquante=data.iloc[40:50]
soixante=data.iloc[50:60]
soixante_dix=data.iloc[60:70]
quatre_vingt=data.iloc[70:80]
quatre_vingt_dix=data.iloc[80:90]
cent=data.iloc[90:100]
cent_sept=data.iloc[100:107]
dix.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"], label=("Hollande","sarkozy"),figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
plt.savefig('static/dix')
vingt.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"], figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
plt.savefig('static/vingt')
trente.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"], figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
plt.savefig('static/trente')
quarante.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"], figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
plt.savefig('static/quarante')
cinquante.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"], figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
plt.savefig('static/cinquante')
soixante.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"], figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
plt.savefig('static/soixante')
soixante_dix.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"], figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
plt.savefig('static/soixante_dix')
quatre_vingt.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"], figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
plt.savefig('static/quatre_vingt')
quatre_vingt_dix.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"], figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
plt.savefig('static/quatre_vingt_dix')
cent.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"], figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
plt.savefig('static/cent')
cent_sept.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"], figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
plt.savefig('static/cent_sept')
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
print("")
data.plot(x="Libellé du départementT1", y=["rHollandeT2", "rSarkozyT2"],
label=("Hollande","sarkozy"),figsize=(20,5), kind="bar", stacked=True)
plt.title('Réparition des votes au second tour par département')
plt.xlabel('Département')
plt.ylabel('Pourcentage de vote')
print("")
plt.savefig('static/Réparition des votes au second tour par département')
# Graphe de corrélation (nuage de point = scatter plot)
data.plot(x="rHollandeT1", y="rHollandeT2", figsize=(16,5),
kind="scatter", label="Hollande", title="correlation")
plt.title('Corrélation des votes entre le premier tour et le second tour pour Hollande')
plt.xlabel('Pourcentage de vote au premier tour')
plt.ylabel('Pourcentage de vote au deuxiéme tour')
plt.savefig('static/Corrélation des votes entre le premier tour et le second tour pour Hollande');
# Superposition de graphes ; 2 scatter plot avec des couleurs diff et une légende
ax=data.plot(x="rHollandeT1", y="rHollandeT2", figsize=(16,5),
kind="scatter", label="Hollande", title="Corrélation des votes entre le premier tour et le second tour pour Hollande et Sarkozy")
data.plot(x="rSarkozyT1", y="rSarkozyT2", kind="scatter", label="Sarkozy", ax=ax, c="red");
plt.xlabel('Pourcentage de vote au premier tour')
plt.ylabel('Pourcentage de vote au deuxiéme tour')
plt.savefig('static/Corrélation des votes entre le premier tour et le second tour pour Hollande et Sarkozy');
# Ajouter une ligne sur un graphe
ax=data.plot(x="rHollandeT1", y="rHollandeT2", figsize=(16,5),
kind="scatter", label="Hollande", title="Vérification de la cohérence des votes (corrélation positive)")
data.plot(x="rSarkozyT1", y="rSarkozyT2", kind="scatter", label="Sarkozy", ax=ax, c="red");
plt.xlabel('Pourcentage de vote au premier tour')
plt.ylabel('Pourcentage de vote au deuxiéme tour')
ax.plot([0.2,0.7], [0.2,0.7], "g--")
ax.text(0.5, 0.65, "Corrélation positive", weight="bold", rotation="17")
ax.text(0.5, 0.5, "Corrélation négative", weight="bold", rotation="17")
plt.savefig('static/Vérification de la cohérence des votes (corrélation positive)');
# plusieurs graphes sur la mâme figure
fig, axes = plt.subplots(1, 2, figsize=(16,5), sharey=True)
data.plot(x="rHollandeT1", y="rHollandeT2", kind="scatter",
label="Hollande", ax=axes[0])
plt.xlabel('Pourcentage de vote au premier tour')
plt.ylabel('Pourcentage de vote au deuxiéme tour')
data.plot(x="rSarkozyT1", y="rSarkozyT2", kind="scatter",
label="Sarkozy", ax=axes[1], c="red")
plt.xlabel('Pourcentage de vote au premier tour')
plt.ylabel('Pourcentage de vote au deuxiéme tour')
print('')
plt.savefig('static/Corrélation des votes entre le premier tour et le second tour pour Hollande et Sarkozy_2');
fig, axes = plt.subplots(1, 1, figsize=(16,5))
c = axes.scatter(x=data["rHollandeT1"],
y=data["rHollandeT2"],
s=data["VotantsT1"]/5000, alpha=0.5)
plt.xlabel('Pourcentage de vote au premier tour')
plt.ylabel('Pourcentage de vote au deuxiéme tour')
axes.plot([0.15,0.7], [0.15,0.7], "g--")
axes.legend( (c,), ("Hollande",) );
Joly=sum(tour1_2["VoixT1"])/1000000
Lepen=sum(tour1_2["Voix.1T1"])/1000000
Melenchon=sum(tour1_2["Voix.3"])/1000000
Arthaud=sum(tour1_2["Voix.5"])/1000000
Cheminade=sum(tour1_2["Voix.6"])/1000000
Bayrou=sum(tour1_2["Voix.7"])/1000000
Dupont=sum(tour1_2["Voix.8"])/1000000
Poutou=sum(tour1_2["Voix.4"])/1000000
HollandeT1=sum(tour1_2["Voix.9"])/1000000
SarkozyT1=sum(tour1_2["Voix.2"])/1000000
NulT1=sum(tour1_2["Blancs et nulsT1"])/1000000
colors = ['b', 'c', 'y', 'm', 'r', 'g', 'c', 'yellowgreen', 'y', 'k', 'w']
HT1 = plt.scatter (2, HollandeT1, marker='x', color=colors[0])
ST1 = plt.scatter(3, SarkozyT1, marker='o', color=colors[1])
JT1 = plt.scatter(4, Joly, marker='*', color=colors[2])
LT1 = plt.scatter(5, Lepen, marker='v', color=colors[3])
MT1 = plt.scatter(6, Melenchon, marker='P', color=colors[4])
AT1 = plt.scatter(7, Arthaud, marker='^', color=colors[5])
CT1 = plt.scatter(8, Cheminade, marker='H', color=colors[6])
BT1 = plt.scatter(9, Bayrou, marker='D', color=colors[7])
DT1 = plt.scatter(10, Dupont, marker='8', color=colors[8])
PT1 = plt.scatter(11, Poutou, marker='s', color=colors[9])
NT1 = plt.scatter(1, NulT1, marker='p', color=colors[10])
plt.legend((HT1, ST1, JT1, LT1, MT1, AT1, CT1, BT1, DT1, PT1,NT1),
('Hollande', 'Sarkozy','Joly','Lepen','Melenchon','Arthaud','Cheminade','Bayrou','Dupont', 'Poutou','Nul'),
scatterpoints=1,
loc='upper right',
ncol=3,
fontsize=8,title="Répartition des votes au premier tour")
plt.xlabel('Candidats')
plt.ylabel('Nombre de voix (en million)')
print("")
plt.savefig('static/Répartition des votes au premier tour_scatter');
colors = ['b', 'c', 'y', 'm', 'r', 'g', 'c', 'yellowgreen', 'y', 'k', 'w']
HT1 = plt.bar (2, HollandeT1, color=colors[0])
ST1 = plt.bar(3, SarkozyT1, color=colors[1])
JT1 = plt.bar(4, Joly, color=colors[2])
LT1 = plt.bar(5, Lepen, color=colors[3])
MT1 = plt.bar(6, Melenchon, color=colors[4])
AT1 = plt.bar(7, Arthaud, color=colors[5])
CT1 = plt.bar(8, Cheminade, color=colors[6])
BT1 = plt.bar(9, Bayrou, color=colors[7])
DT1 = plt.bar(10, Dupont, color=colors[8])
PT1 = plt.bar(11, Poutou, color=colors[9])
NT1 = plt.bar(1, NulT1, color=colors[10])
plt.legend((HT1, ST1, JT1, LT1, MT1, AT1, CT1, BT1, DT1, PT1,NT1),
('Hollande', 'Sarkozy','Joly','Lepen','Melenchon','Arthaud','Cheminade','Bayrou','Dupont', 'Poutou','Nul'),
scatterpoints=1,
loc='upper right',
ncol=3,
fontsize=8,title="Répartition des votes au premier tour")
plt.xlabel('Candidats')
plt.ylabel('Nombre de voix (en million)')
print("")
plt.savefig('static/Répartition des votes au premier tour_plot');
colors = ['r','b', 'c', 'y', 'm', 'r', 'g', 'c', 'yellowgreen', 'y', 'k']
slices=[NulT1,HollandeT1,Joly,SarkozyT1,Lepen,Cheminade,Melenchon,Arthaud,Bayrou,Dupont,Poutou]
labels=['Nul','Hollande','Joly', 'Sarkozy','Lepen','Cheminade','Melenchon','Arthaud','Bayrou','Dupont', 'Poutou']
plt.pie (slices,labels=labels,colors=colors, shadow = True, autopct='%1.1f%%',explode = (0, 0.1, 0, 0,0,0,0,0,0,0,0))
plt.title("Répartition des votes au premier tour", fontsize=20)
plt.gcf().set_size_inches(10,10)
print("")
plt.savefig('static/Répartition des votes au premier tour_pie');
plt.boxplot([[NulT1,HollandeT1,Joly,SarkozyT1,Lepen,Cheminade,Melenchon,Arthaud,Bayrou,Dupont,Poutou]
])
plt.title("Répartition des votes au premier tour en million", fontsize=20)
plt.ylabel('Nbre de voix', fontsize=12)
print("Voix des candidats au premier tour")
df = pd.DataFrame({'En millions':[NulT1,HollandeT1,Joly,SarkozyT1,Lepen,Cheminade,Melenchon,Arthaud,Bayrou,Dupont,Poutou]})
print(df.describe())
plt.savefig('static/Répartition des votes au premier tour_boxplot')
chomage = pd.read_excel('source/chomage.xls',sheet_name = 'Département', index_col = None)
chomage.rename(columns={'T2_2012':'Taux_chomage_T2_2012'}, inplace=True)
chomage_2012=chomage[['Code du département','Libellé', 'Taux_chomage_T2_2012' ]]
departement_elections_tour1=pd.read_excel('source/resultat-elections-2012.xls',sheet_name = 'Départements T1', index_col = None)
departement_elections_tour2=pd.read_excel('source/resultat-elections-2012.xls',sheet_name = 'Départements T2', index_col = None)
chomage_tour1 = departement_elections_tour1.merge(chomage_2012, left_on = 'Code du département', right_on = 'Code du département')
chomage_tour1.to_excel("source/temp/chomage_tour1.xlsx")
chomage_tou1_mini=chomage_tour1[['% Abs/Ins','% Vot/Ins', '% Exp/Ins', '% Exp/Vot', '% Voix/Exp', '% Voix/Exp.1', '% Voix/Exp.2', '% Voix/Exp.3','% Voix/Exp.4','% Voix/Exp.5','% Voix/Exp.6','% Voix/Exp.7','% Voix/Exp.8','% Voix/Exp.9','Taux_chomage_T2_2012' ]]
chomage_tou1_mini.to_excel("source/temp/chomage_tou1_mini.xlsx")
chomage_tour2 = departement_elections_tour2.merge(chomage_2012, left_on = 'Code du département', right_on = 'Code du département')
chomage_tour2.to_excel("source/temp/chomage_tour2.xlsx")
print("Correlations entre votes et chômage au premier tour:")
sns.heatmap(chomage_tou1_mini.corr(method='pearson'),annot=True,cmap='RdYlGn',linewidths=0.2,annot_kws={'size':20})
fig=plt.gcf()
fig.set_size_inches(20,20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
chaine = "\nOn observe une forte corrélation entre le taux de chomage et \nle nombre de personnes ayant voté pour Le Pen, puis un peu moins \npour Mélanchon et enfin pour l'abstention.\nOn remarque aussi que la corrélation est trés négative \nentre le taux de chomage et Bayrou et deux fois moins avec Sarkozy et Dupont-Aignan au premier tour."
print(chaine)
plt.savefig('static/Correlations entre votes et chômage au premier tour')
chomage_tour1.plot(x="Libellé du département", y=["% Voix/Ins.9","% Voix/Ins.2","% Voix/Ins.1","% Voix/Ins.3" ,"% Abs/Ins","% Voix/Ins.7","% Voix/Ins.8","Taux_chomage_T2_2012"], label=("Hollande","sarkozy","Le Pen","Mélanchon", "Abstention","Bayrou","Dupont-Aignan","Taux de chomage"),figsize=(16,5))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au premier tour et taux de chomage par département (en %)')
plt.xlabel('Département')
plt.ylabel('Pourcentage')
plt.savefig('static/Réparition des votes au premier tour et taux de chomage par département (en %)')
chomage_tour2.plot(x="Libellé du département", y=["% Voix/Exp","% Voix/Exp.1","Taux_chomage_T2_2012"], label=("Hollande","sarkozy","Taux de chomage"),figsize=(8,4))
plt.xticks(rotation='vertical')
plt.title('Réparition des votes au second tour et taux de chomage par département (en %)')
plt.xlabel('Département')
plt.ylabel('Pourcentage')
plt.savefig('static/Réparition des votes au second tour et taux de chomage par département (en %)')
print("Correlations entre votes et chômage au deuxiéme tour:")
sns.heatmap(chomage_tour2.corr(method='pearson'),annot=True,cmap='RdYlGn',linewidths=0.2,annot_kws={'size':20})
fig=plt.gcf()
fig.set_size_inches(20,20)
plt.xticks(fontsize=20)
plt.yticks(fontsize=20)
chaine = "\nOn observe une légére corrélation entre le taux de chomage et \nl'abstention ainsi que les votes nuls.\nOn remarque aussi que la corrélation est deux fois plus forte \nentre le taux de chomage et Hollande qu'entre le taux de chomage et Sarkozy"
print(chaine)
plt.savefig('static/Correlations entre votes et chômage deuxieme tour')
print("Tableau des corrélations entre votes et chomages")
data.corr(method='pearson')
tour2['% Voix/Exp'].isna().sum()
tour2['% Voix/Exp'].fillna(method='ffill', inplace=True)
tour2['% Voix/Exp'].isna().sum()
tour2.to_excel("source/temp/tour2.xlsx")
state_geo = f'https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/departements.geojson'
state_data = pd.read_excel('source/temp/tour2.xlsx')
state_data = state_data.to_csv('source/temp/tour2.csv', encoding='utf-8')
state_data = pd.read_csv('source/temp/tour2.csv')
map1 = folium.Map(location=[48.866667, 2.333333],
zoom_start = 5)
folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=state_data,
columns=['Code du département', '% Voix/Exp'],
key_on='feature.properties.code',
fill_color='BuPu', nan_fill_color='white',
fill_opacity=0.6, nan_fill_opacity=None,
line_color='black', line_weight=1, line_opacity=1,
legend_name='Pourcentage de vote pour Hollande au deuxième tour par département'
).add_to(map1)
folium.LayerControl().add_to(map1)
print("Pourcentage de vote pour Hollande au deuxième tour par département")
map1
map1.save('templates/map1.html')
state_geo = f'https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/regions.geojson'
state_data = pd.read_excel('source/temp/tour2.xlsx')
state_data = state_data.to_csv('source/temp/tour2.csv', encoding='utf-8')
state_data = pd.read_csv('source/temp/tour2.csv')
map1_1 = folium.Map(location=[48.866667, 2.333333],
zoom_start = 5)
folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=state_data,
columns=['Code du département', '% Voix/Exp'],
key_on='feature.properties.code',
fill_color='OrRd', nan_fill_color='white',
fill_opacity=0.6, nan_fill_opacity=None,
line_color='black', line_weight=1, line_opacity=1,
legend_name='Pourcentage de vote pour Hollande, au deuxième tour, par région'
).add_to(map1_1)
folium.LayerControl().add_to(map1_1)
print("Pourcentage de vote pour Hollande, au deuxième tour, par région")
map1_1
map1_1.save('templates/map1_1.html')
state_geo = f'https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/departements.geojson'
state_data = pd.read_excel('source/temp/tour2.xlsx')
state_data = state_data.to_csv('source/temp/tour2.csv', encoding='utf-8')
state_data = pd.read_csv('source/temp/tour2.csv')
map1_2 = folium.Map(location=[48.866667, 2.333333],
zoom_start = 5)
folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=state_data,
columns=['Code du département', '% Voix/Exp.1'],
key_on='feature.properties.code',
fill_color='BuPu', nan_fill_color='white',
fill_opacity=0.6, nan_fill_opacity=None,
line_color='black', line_weight=1, line_opacity=1,
legend_name='Pourcentage de vote pour Sarkozy au deuxième tour par département'
).add_to(map1_2)
folium.LayerControl().add_to(map1_2)
print("Pourcentage de vote pour Sarkozy au deuxième tour par département")
map1_2
map1_2.save('templates/map1_2.html')
state_geo = f'https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/regions.geojson'
state_data = pd.read_excel('source/temp/tour2.xlsx')
state_data = state_data.to_csv('source/temp/tour2.csv', encoding='utf-8')
state_data = pd.read_csv('source/temp/tour2.csv')
map1_3 = folium.Map(location=[48.866667, 2.333333],
zoom_start = 5)
folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=state_data,
columns=['Code du département', '% Voix/Exp.1'],
key_on='feature.properties.code',
fill_color='GnBu', nan_fill_color='white',
fill_opacity=0.6, nan_fill_opacity=None,
line_color='black', line_weight=1, line_opacity=1,
legend_name='Pourcentage de vote pour Sarkozy au deuxième tour par région'
).add_to(map1_3)
folium.LayerControl().add_to(map1_3)
print("Pourcentage de vote pour Sarkozy au deuxième tour par région")
map1_3
map1_3.save('templates/map1_3.html')
tour2['diff']=tour2['Voix']-tour2['Voix.1']
def win(diff):
if diff > 0:
res = 1
else:
res = 0
return res
tour2['Winner'] = list(map(win, tour2['diff']))
state_geo = f'https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/departements.geojson'
#state_data = pd.read_excel('source/temp/tour2.xlsx')
state_data = tour2.to_csv('source/temp/tour2.csv', encoding='utf-8')
state_data = pd.read_csv('source/temp/tour2.csv')
map2 = folium.Map(location=[48.866667, 2.333333],
zoom_start = 5)
folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=state_data,
columns=['Code du département', 'Winner'],
key_on='feature.properties.code',
fill_color='BuPu', nan_fill_color='purple',
fill_opacity=0.6, nan_fill_opacity=None,
line_color='black', line_weight=1, line_opacity=1,
legend_name='Votes pour Sarkozy (Blanc) ou Hollande (violet) au deuxieme tour par département'
).add_to(map2)
folium.LayerControl().add_to(map2)
print("Votes pour Sarkozy (Blanc) ou Hollande (violet) au deuxieme tour par département")
map2
map2.save('templates/map2.html')
state_geo = f'https://raw.githubusercontent.com/gregoiredavid/france-geojson/master/regions.geojson'
#state_data = pd.read_excel('source/temp/tour2.xlsx')
state_data = tour2.to_csv('source/temp/tour2.csv', encoding='utf-8')
state_data = pd.read_csv('source/temp/tour2.csv')
map3 = folium.Map(location=[48.866667, 2.333333],
zoom_start = 5)
folium.Choropleth(
geo_data=state_geo,
name='choropleth',
data=state_data,
columns=['Code du département', 'Winner'],
key_on='feature.properties.code',
fill_color='YlGn', nan_fill_color='black',
fill_opacity=0.6, nan_fill_opacity=None,
line_color='black', line_weight=1, line_opacity=1,
legend_name='Votes pour Sarkozy (Blanc) ou Hollande (vert) au deuxieme tour par région'
).add_to(map3)
folium.LayerControl().add_to(map3)
print("Votes pour Sarkozy (Blanc) ou Hollande (vert) au deuxieme tour par région")
map3
map3.save('templates/map3.html')
webbrowser.open('templates/map1.html')
webbrowser.open('templates/map1_1.html')
webbrowser.open('templates/map1_2.html')
webbrowser.open('templates/map1_3.html')
webbrowser.open('templates/map2.html')
webbrowser.open('templates/map3.html')
Créer PDF
A executer sur le terminal sudo apt-get install texlive-xetex sudo apt-get install pandoc
pip install nbconvert
conda install nbconvert EXECUTER CETTE LIGNE SUR LE TERMINAL jupyter nbconvert --to pdf --TemplateExporter.exclude_input=False "Projet groupe éléctions présidentielles.ipynb"